﻿using System;
using System.Windows.Forms;
using Group_Mail.Controls;
using mshtml;

namespace Group_Mail
{
    public partial class frmTemplateCreator : Form
    {
        private readonly CheckBoxToolStripItem chkAutoPlainText;
        private bool saveClose;

        public frmTemplateCreator()
        {
            InitializeComponent();
            chkAutoPlainText = new CheckBoxToolStripItem {Text = "Auto plaint text"};
            chkAutoPlainText.CheckedChanged += chkAutoPlainText_CheckedChanged;
            toolStrip.Items.Insert(8, chkAutoPlainText);
        }

        public frmTemplateCreator(Template template) : this()
        {
            Template = template;
        }

        public Template Template { get; set; }

        private void chkAutoPlainText_CheckedChanged(object sender, EventArgs e)
        {
            if (tabTemplateBody.SelectedIndex != 0)
            {
                tabTemplateBody.SelectedIndex = 0;
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Please insert Html Rich Text into this box", "Template Creator", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
        }

        private void btnPaste_Click(object sender, EventArgs e)
        {
            var data = Clipboard.GetDataObject();
            if (data.GetDataPresent(DataFormats.Html))
            {
                var html = (string) data.GetData(DataFormats.Html);
                webBrowser.DocumentText = html;
            }
        }

        private void frmTemplateCreator_Load(object sender, EventArgs e)
        {
            webBrowser.DocumentText = "<html><body></body></html>";
            var doc = webBrowser.Document.DomDocument as IHTMLDocument2;
            doc.designMode = "On";
            webBrowser.Document.ContextMenuShowing += Document_ContextMenuShowing;
        }

        private static void Document_ContextMenuShowing(object sender, HtmlElementEventArgs e)
        {
            e.ReturnValue = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ReturnTemplate())
            {
                saveClose = true;
                Close();
            }
        }

        private void tabTemplateBody_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (chkAutoPlainText.Checked && tabTemplateBody.SelectedIndex != 0)
            {
                MessageBox.Show("Cannot edit plaint text when using auto plaint text mode", "Group Mail",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                tabTemplateBody.SelectedIndex = 0;
            }
        }

        private bool ReturnTemplate()
        {
            if (Template == null)
            {
                Template = new Template();
            }

            Template.Name = txtName.Text.Trim();
            Template.AutoPlainText = chkAutoPlainText.Checked;
            Template.HtmlBody = webBrowser.Document.Body.InnerHtml;
            Template.PlainTextBody = txtPlainText.Text;

            if (string.IsNullOrEmpty(Template.Name))
            {
                MessageBox.Show("Please enter template name", "Group Mail",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(Template.Id))
            {
                Template.Id = Guid.NewGuid().ToString();
            }

            DialogResult = DialogResult.OK;

            return true;
        }

        private void frmTemplateCreator_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!saveClose)
            {
                var result = MessageBox.Show("Do you want to save the template?", "Group Mail",
                                             MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                switch (result)
                {
                    case DialogResult.Yes:
                        e.Cancel = !ReturnTemplate();
                        break;
                    case DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                }
            }
        }
    }
}